//
//  MBSpaceCollectionMusicCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/21.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceCollectionMusicCell.h"

@interface MBSpaceCollectionMusicCell () {
    UIView                      *_bgView;
    UIButton                    *_selectBt;
    
    UIView                      *_contentView;
    UILabel                     *_nickNameLb;
    UIImageView                 *_downImgView;
    UIView                      *_musicView;
    UILabel                     *_musicSizeLb;
    UILabel                     *_musicTypeLb;
    UILabel                     *_musicPlayCountLb;
    UIView                      *_lineView;
    
    UIButton                    *_moreBt;
}

@property (nonatomic,assign)BOOL isCompliling;   //是否编译
@property (nonatomic,assign)BOOL isALlSelecting; //是否全选

@end
@implementation MBSpaceCollectionMusicCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    //勾选按钮
    _selectBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectBt.backgroundColor = [UIColor clearColor];
    _selectBt.hidden = YES;
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateHighlighted];
    [_selectBt addTarget:self action:@selector(onGouXuanToSelectAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_selectBt];
    
    //大模块 包含音乐标题、大小、类型以及下载图标
    _contentView = [[UIView alloc]initWithFrame:CGRectZero];
    _contentView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:_contentView];
    
    //标题
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    [_contentView addSubview:_nickNameLb];
    
    //下载图标
    _downImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _downImgView.backgroundColor = [UIColor clearColor];
    _downImgView.hidden = YES;
    [_contentView addSubview:_downImgView];
    
    //小模块 包含歌曲大小、类型
    _musicView = [[UIView alloc]initWithFrame:CGRectZero];
    _musicView.backgroundColor = [UIColor whiteColor];
    [_contentView addSubview:_musicView];
    
    //大小
    _musicSizeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _musicSizeLb.backgroundColor = [UIColor clearColor];
    _musicSizeLb.textAlignment = NSTextAlignmentLeft;
    _musicSizeLb.textColor = UIColorFromHex(0x999999);
    _musicSizeLb.font = [UIFont systemFontOfSize:11.f];
    [_musicView addSubview:_musicSizeLb];
    
    //类型
    _musicTypeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _musicTypeLb.backgroundColor = [UIColor clearColor];
    _musicTypeLb.textAlignment = NSTextAlignmentLeft;
    _musicTypeLb.textColor = UIColorFromHex(0x9999999);
    _musicTypeLb.font = [UIFont systemFontOfSize:11.f];
    [_musicView addSubview:_musicTypeLb];
    
    //试听次数
    _musicPlayCountLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _musicPlayCountLb.backgroundColor = [UIColor clearColor];
    _musicPlayCountLb.textAlignment = NSTextAlignmentLeft;
    _musicPlayCountLb.textColor = UIColorFromHex(0x9999999);
    _musicPlayCountLb.font = [UIFont systemFontOfSize:11.f];
    _musicPlayCountLb.hidden = YES;
    [_musicView addSubview:_musicPlayCountLb];
    
    //线
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0x999999) colorWithAlphaComponent:.35];
    [_bgView addSubview:_lineView];
    
    //更多按钮
    _moreBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _moreBt.backgroundColor = [UIColor clearColor];
    [_moreBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [_moreBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateHighlighted];
    [_moreBt addTarget:self action:@selector(onMoreToSelectAction) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_moreBt];
}

//更多选择
- (void)onMoreToSelectAction
{
    if (_moreBt.isHidden == NO) {
        if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForMoewWithValue:withView:)]) {
            if (_dateDic) {
                [_delegate onSelectItemForMoewWithValue:_dateDic withView:self];
            }
            else if (_downedDic) {
                [_delegate onSelectItemForMoewWithValue:_downedDic withView:self];
            }
            else if (_musicDic) {
                [_delegate onSelectItemForMoewWithValue:_musicDic withView:self];
            }
            else if (IS_TEXT.length > 0) {
                [_delegate onSelectItemForMoewWithValue:@"" withView:self];
            }
        }
    }
}

//单个勾选
- (void)onGouXuanToSelectAction:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForGouXuanWithValue:WithSelect:WithView:)]) {
        bt.selected = !bt.selected;
        if (_dateDic) {
            [_delegate onSelectItemForGouXuanWithValue:_dateDic WithSelect:bt.selected WithView:self];
        }
        else if (_downedDic) {
            [_delegate onSelectItemForGouXuanWithValue:_downedDic WithSelect:bt.selected WithView:self];
        }
        else if (_musicDic) {
            [_delegate onSelectItemForGouXuanWithValue:_musicDic WithSelect:bt.selected WithView:self];
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)onCompileCellForLayout:(BOOL)isCompile WithAllSelct:(BOOL)isAllSelct
{
    self.isCompliling = isCompile;
    self.isALlSelecting = isAllSelct;
    [self setNeedsLayout];
}

- (void)setIsCompliling:(BOOL)isCompliling
{
    _isCompliling = isCompliling;
    [self setNeedsLayout];
}

- (void)setIsALlSelecting:(BOOL)isALlSelecting
{
    _isALlSelecting = isALlSelecting;
    [self setNeedsLayout];
}

- (void)setIsHideMoewBt:(BOOL)isHideMoewBt
{
    _isHideMoewBt = isHideMoewBt;
    [self setNeedsLayout];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setDateDic:(NSDictionary *)dateDic
{
    _dateDic = dateDic;
    [self setNeedsLayout];
}

- (void)setDownedDic:(NSDictionary *)downedDic
{
    _downedDic = downedDic;
    [self setNeedsLayout];
}

- (void)setIsShowPlayCount:(BOOL)isShowPlayCount
{
    _isShowPlayCount = isShowPlayCount;
    [self setNeedsLayout];
}

- (void)setIsShowDowned:(BOOL)isShowDowned
{
    _isShowDowned = isShowDowned;
    [self setNeedsLayout];
}

- (void)setMusicDic:(NSDictionary *)musicDic
{
    _musicDic = musicDic;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //value
    UIImage *selectNoImg = [UIImage imageNamed:@"weigouxuan_fb"];
    UIImage *selectImg = [UIImage imageNamed:@"xuanzhong_zl"];
    [_selectBt setImage:selectNoImg forState:UIControlStateNormal];
    [_selectBt setImage:selectImg forState:UIControlStateSelected];
    _selectBt.selected = self.isALlSelecting;
    
    UIImage *img = [UIImage imageNamed:@"gouxuan_fb"];
    _downImgView.image = img;
    if (_downedDic) {
        //已下载 儿歌、国学等空间包
        _downImgView.hidden = NO;
        if ([_downedDic.allKeys containsObject:@"AudioName"]) {
            NSString *audioName = [_downedDic objectForKey:@"AudioName"];
            NSArray *audioNameArr = [audioName componentsSeparatedByString:@"."];
            _nickNameLb.text = audioNameArr[0];
            _musicSizeLb.text = [NSString stringWithFormat:@"%@",[_downedDic objectForKey:@"Capacity"]];
            NSString *musicType = [NSString stringWithFormat:@"%@",[_downedDic objectForKey:@"ValueID"]];
            _musicTypeLb.text = [musicType showMusicType];

        }
        else if ([_downedDic.allKeys containsObject:@"fileName"]) {
            _nickNameLb.text = [_downedDic objectForKey:@"fileName"];
            _musicSizeLb.text = [_downedDic objectForKey:@"size"];
            NSString *musicType = [NSString stringWithFormat:@"%@",[_downedDic objectForKey:@"type"]];
            _musicTypeLb.text = [musicType showMusicType];
        }
        else if ([_downedDic.allKeys containsObject:@"title"]) {
            _nickNameLb.text = [_downedDic objectForKey:@"title"];
            _musicSizeLb.text = [_downedDic objectForKey:@"size"];
            NSString *musicType = [NSString stringWithFormat:@"%@",[_downedDic objectForKey:@"type"]];
            _musicTypeLb.text = [musicType showMusicType];
        }
        else if ([_downedDic.allKeys containsObject:@"ShowTitle"]) {
            _nickNameLb.text = [_downedDic objectForKey:@"ShowTitle"];
            _musicSizeLb.text = [NSString stringWithFormat:@"%@",[_downedDic objectForKey:@"Capacity"]];
            NSString *musicType = [NSString stringWithFormat:@"%@",[_downedDic objectForKey:@"ShowType"]];
            if ([musicType intValue] == 1) {
                musicType = @"5";
            } else {
                musicType = @"6";
            }
            _musicTypeLb.text = [musicType showMusicType];
        }
    }
    else if (_dateDic) {
        _downImgView.hidden = YES;
        NSString *audioName = [_dateDic objectForKey:@"AudioName"];
        NSArray *audioNameArr = [audioName componentsSeparatedByString:@"."];
        _nickNameLb.text = audioNameArr[0];
        NSString *musicType = [NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"ValueID"]];
        _musicTypeLb.text = [musicType showMusicType];
        _musicPlayCountLb.text = [NSString stringWithFormat:@"试听%@次",[self getPlayCount:[_dateDic objectForKey:@"AuditionNum"]]];

    }
    else if (_musicDic) {
        _downImgView.hidden = YES;
        if ([_musicDic.allKeys containsObject:@"AudioName"]) {
            NSString *audioName = [_musicDic objectForKey:@"AudioName"];
            NSArray *audioNameArr = [audioName componentsSeparatedByString:@"."];
            _nickNameLb.text = audioNameArr[0];
            _musicSizeLb.text = [NSString stringWithFormat:@"共%@M",[_musicDic objectForKey:@"Capacity"]];
            NSString *musicType = [NSString stringWithFormat:@"%@",[_musicDic objectForKey:@"ValueID"]];
            _musicTypeLb.text = [musicType showMusicType];
        }
        else if ([_musicDic.allKeys containsObject:@"NickName"]) {
            NSString *audioName = [_musicDic objectForKey:@"ShowTitle"];
            _nickNameLb.text = audioName;
            _musicSizeLb.text = [NSString stringWithFormat:@"共%@M",[_musicDic objectForKey:@"Capacity"]];
            NSString *musicType = [NSString stringWithFormat:@"%@",[_musicDic objectForKey:@"ValueID"]];
            _musicTypeLb.text = [musicType showMusicType];
        }
        else if ([_musicDic.allKeys containsObject:@"fileID"]) {
            _downImgView.hidden = NO;
            NSString *audioName = [_musicDic objectForKey:@"fileName"];
            _nickNameLb.text = audioName;
            _musicSizeLb.text = [NSString stringWithFormat:@" %@",[_musicDic objectForKey:@"size"]];
            NSString *musicType = [NSString stringWithFormat:@"%@",[_musicDic objectForKey:@"ValueID"]];
            _musicTypeLb.text = [musicType showMusicType];
        }
        else if ([_musicDic.allKeys containsObject:@"title"]) {
            _downImgView.hidden = NO;
            NSString *audioName = [_musicDic objectForKey:@"title"];
            _nickNameLb.text = audioName;
            _musicSizeLb.text = [NSString stringWithFormat:@" %@",[_musicDic objectForKey:@"size"]];
            NSString *musicType = [NSString stringWithFormat:@"%@",[_musicDic objectForKey:@"type"]];
            _musicTypeLb.text = [musicType showMusicType];
        }
    } else {
        //测试默认数据
        _downImgView.hidden = YES;
        _nickNameLb.text = @"摇篮曲";
        _musicSizeLb.text = @"1.3M";
        _musicTypeLb.text = @"歌曲";
    }
    
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);
    _selectBt.frame = CGRectMake((ISIPAD ?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (_bgView.height-selectNoImg.size.height)/2., selectNoImg.size.width, selectNoImg.size.height);
    _selectBt.hidden = !_isCompliling;
   
    _contentView.frame = CGRectMake((_selectBt.isHidden ? _selectBt.left : _selectBt.right+5), 0, _bgView.width-125*PERCEN, _bgView.height);
    _nickNameLb.frame = CGRectMake(0, (ISIPAD?20*PERCEN:(WIDTH_5S ?8*PERCEN:14*PERCEN)), _contentView.width, 20);
    
    _downImgView.frame = CGRectMake(_nickNameLb.left, _nickNameLb.bottom+12*PERCEN, img.size.width/2., img.size.height/2.);
    _downImgView.hidden = !_isShowDowned;
    _musicView.frame = CGRectMake((_downImgView.isHidden?_nickNameLb.left:_downImgView.right+4*PERCEN), _nickNameLb.bottom, 200, 20);
    
    float musicWidth = [_musicSizeLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_musicSizeLb.font} context:NULL].size.width+6;
    _musicSizeLb.frame = CGRectMake(0, 0, musicWidth, 20);
    
    float musicTypeWidth = [_musicTypeLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_musicTypeLb.font} context:NULL].size.width;
    _musicTypeLb.frame = CGRectMake((_musicSizeLb.width==6?0:_musicSizeLb.right+10*PERCEN), 0, musicTypeWidth, 20);
    
    _musicPlayCountLb.frame = CGRectMake(_musicTypeLb.right+20*PERCEN, _musicTypeLb.top, 120, _musicTypeLb.height);
    _musicPlayCountLb.hidden = !_isShowPlayCount;
    
    _lineView.frame = CGRectMake((ISIPAD?440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), _bgView.height-1, (ISIPAD?_bgView.width-2*440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):_bgView.width), 1);
    
    _moreBt.frame = CGRectMake(_bgView.width-100*PERCEN-(ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), (_bgView.height-20*PERCEN)/2., 100*PERCEN, 20*PERCEN);
    _moreBt.hidden = (_isHideMoewBt == NO ? !_selectBt.isHidden : YES);

}

- (NSString *)getPlayCount:(NSString *)playCount
{
    int count = [playCount intValue];
    if (count / 10000 > 0) {
        return [NSString stringWithFormat:@"%d.%d万",count/10000,count/1000];
    }
    else if (count / 1000 > 0) {
        return [NSString stringWithFormat:@"%d.%d千",count/1000,count/100];
    }
    return [NSString stringWithFormat:@"%d",count];
}

@end
